package cn.com.xf.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import cn.com.xf.dao.TblCheckDataDAO;
import cn.com.xf.dao.support.XFSqlMapClientDaoSupport;
import cn.com.xf.domain.Tabackorder;
import cn.com.xf.domain.Tbareainfo;
import cn.com.xf.domain.TblCheckData;

@Repository
public class TblCheckDataDAOImpl extends XFSqlMapClientDaoSupport implements TblCheckDataDAO {

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_DATA
     *
     * @ibatorgenerated Mon Aug 15 15:49:28 CST 2016
     */
    public TblCheckDataDAOImpl() {
        super();
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_DATA
     *
     * @ibatorgenerated Mon Aug 15 15:49:28 CST 2016
     */
    public int deleteByPrimaryKey(Long id) {
        TblCheckData key = new TblCheckData();
        key.setId(id);
        int rows = getSqlMapClientTemplate().delete("TBL_CHECK_DATA.ibatorgenerated_deleteByPrimaryKey", key);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_DATA
     *
     * @ibatorgenerated Mon Aug 15 15:49:28 CST 2016
     */
    public void insert(TblCheckData record) {
        getSqlMapClientTemplate().insert("TBL_CHECK_DATA.ibatorgenerated_insert", record);
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_DATA
     *
     * @ibatorgenerated Mon Aug 15 15:49:28 CST 2016
     */
    public void insertSelective(TblCheckData record) {
        getSqlMapClientTemplate().insert("TBL_CHECK_DATA.ibatorgenerated_insertSelective", record);
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_DATA
     *
     * @ibatorgenerated Mon Aug 15 15:49:28 CST 2016
     */
    public TblCheckData selectByPrimaryKey(Long id) {
        TblCheckData key = new TblCheckData();
        key.setId(id);
        TblCheckData record = (TblCheckData) getSqlMapClientTemplate().queryForObject("TBL_CHECK_DATA.ibatorgenerated_selectByPrimaryKey", key);
        return record;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_DATA
     *
     * @ibatorgenerated Mon Aug 15 15:49:28 CST 2016
     */
    public int updateByPrimaryKeySelective(TblCheckData record) {
        int rows = getSqlMapClientTemplate().update("TBL_CHECK_DATA.ibatorgenerated_updateByPrimaryKeySelective", record);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table TBL_CHECK_DATA
     *
     * @ibatorgenerated Mon Aug 15 15:49:28 CST 2016
     */
    public int updateByPrimaryKey(TblCheckData record) {
        int rows = getSqlMapClientTemplate().update("TBL_CHECK_DATA.ibatorgenerated_updateByPrimaryKey", record);
        return rows;
    }

	@Override
	public List<TblCheckData> select(int begin, int pageSize,String year_Month, String siteid, 
			String deptid, String userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		int end = begin + pageSize;
		begin = begin + 1;
		map.put("begin", begin);
		map.put("end", end);
		String[] siteids = siteid.split(",");
		map.put("year_Month", year_Month);
		map.put("siteids", siteids);
		map.put("deptid", deptid);
		map.put("userid", userid);
		return getSqlMapClientTemplate().queryForList("TBL_CHECK_DATA.select", map);
	}

	@Override
	public int selectCount(String year_Month, String siteid, String deptid, String userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		String[] siteids = siteid.split(",");
		map.put("year_Month", year_Month);
		map.put("siteids", siteids);
		map.put("deptid", deptid);
		map.put("userid", userid);
		return (Integer)getSqlMapClientTemplate().queryForObject("TBL_CHECK_DATA.selectCount", map);
	}

	@Override
	public List<TblCheckData> selectCheckDataByUseridAndDate(String userid, Date checkdate) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("checkdate", checkdate);
		map.put("userid", userid);
		return getSqlMapClientTemplate().queryForList("TBL_CHECK_DATA.selectCheckDataByUseridAndDate", map);
	}

	@Override
	public int insertBatch(List<TblCheckData> datas) {
		int count = 0;
		try {
			// 自己控制事物的开始
			getSqlMapClient().startTransaction();
			getSqlMapClient().startBatch();
			for (TblCheckData data : datas) {
				try {
					getSqlMapClientTemplate().insert("TBL_CHECK_DATA.ibatorgenerated_insertSelective", data);
					count = getSqlMapClient().executeBatch();
					getSqlMapClient().commitTransaction();
				} catch (SQLException e) {
				}
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 自己控制事物的结束
			try {
				getSqlMapClient().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return count;
	}

	@Override
	public List<TblCheckData> selectSummary(int begin, int pageSize,
			String userid, String siteid, String deptid, Date startdate,
			Date enddate, String deviceid) {
		Map<String, Object> map = new HashMap<String, Object>();
		int end = begin + pageSize;
		begin = begin + 1;
		map.put("begin", begin);
		map.put("end", end);
		map.put("userid", userid);
		map.put("siteid", siteid);
		map.put("deptid", deptid);
		map.put("startdate", startdate);
		map.put("enddate", enddate);
		map.put("deviceid", deviceid);
		return getSqlMapClientTemplate().queryForList("TBL_CHECK_DATA.selectSummary", map);
	}

	@Override
	public int selectSummaryCount(String userid, String siteid, String deptid,
			Date startdate, Date enddate, String deviceid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userid", userid);
		map.put("siteid", siteid);
		map.put("deptid", deptid);	
		map.put("startdate", startdate);
		map.put("enddate", enddate);
		map.put("deviceid", deviceid);
		return (Integer)getSqlMapClientTemplate().queryForObject("TBL_CHECK_DATA.selectSummaryCount", map);
	}

	@Override
	public List<TblCheckData> selectUser(String userid, String deviceid,
			Date startdate, Date enddate) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userid", userid);
		map.put("deviceid", deviceid);
		map.put("startdate", startdate);
		map.put("enddate", enddate);
		return getSqlMapClientTemplate().queryForList("TBL_CHECK_DATA.selectUser", map);
	}


	
	@Override
	public List<TblCheckData> selectExport(String year_Month, String siteid, String deptid, String userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		String[] siteids = siteid.split(",");
		map.put("year_Month", year_Month);
		map.put("siteids", siteids);
		map.put("deptid", deptid);
		map.put("userid", userid);
		return getSqlMapClientTemplate().queryForList("TBL_CHECK_DATA.selectExport", map);
	}
	@Override
	public List<TblCheckData> selectDistinct(String year_Month, String siteid, String deptid, String userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		String[] siteids = siteid.split(",");
		map.put("year_Month", year_Month);
		map.put("siteids", siteids);
		map.put("deptid", deptid);
		map.put("userid", userid);
		return getSqlMapClientTemplate().queryForList("TBL_CHECK_DATA.selectDistinct", map);
	}
	/** 查询考勤明细总数
	 * @author  llh
	 */
	@Override
	public int selectDatailReportListCount(String year_Month, String siteid, String deptid) {
		Map<String, Object> map = new HashMap<String, Object>();
		String[] siteids = siteid.split(",");
		map.put("siteids", siteids);
		map.put("deptid", deptid);
		map.put("year_Month", year_Month);
		
		return (Integer)getSqlMapClientTemplate().queryForObject("TBL_CHECK_DATA.selectDatailReportListCount", map);
	}
	/** 查询考勤明细
	 * @author  llh
	 */
	@Override
	public List<TblCheckData> selectDatailReportList(int begin, int pageSize, String year_Month, String siteid, String deptid){
		Map<String, Object> map = new HashMap<String, Object>();
		int end = begin + pageSize;
		begin = begin + 1;
		map.put("begin", begin);
		map.put("end", end);
		String[] siteids = siteid.split(",");
		map.put("year_Month", year_Month);
		map.put("siteids", siteids);
		map.put("deptid", deptid);
		map.put("siteid", siteid);
		
		return getSqlMapClientTemplate().queryForList("TBL_CHECK_DATA.selectDatailReportList", map);
	}

	@Override
	public List<TblCheckData> selectCheckDataByUseridsAndDate(String[] userids, Date startDate, Date endDate) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("userids", userids);
		return getSqlMapClientTemplate().queryForList("TBL_CHECK_DATA.selectCheckDataByUseridsAndDate", map);
	}

	@Override
	public Map<String, Object> getSitesCheckReport(Map<String, Object> paramMap) {
		String retcode = null;
		String errmsg = null;
		ResultSet retcur = null;
		paramMap.put("RetValue", retcode);
		paramMap.put("RetErr", errmsg);
		paramMap.put("v_cur", retcur);
		getSqlMapClientTemplate().queryForObject("TBL_CHECK_DATA.Prm_AttendStat_T", paramMap);
		paramMap.put("result", paramMap.get("v_cur"));
		paramMap.remove("RetValue");
		paramMap.remove("RetErr");
		paramMap.remove("ASiteID");
		paramMap.remove("AEnterBeginDate");
		paramMap.remove("AEnterEndDate");
		return paramMap;
	}

}